package com.example.wangandroid.ui.login.ui.fragment

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.viewpager2.widget.ViewPager2
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.blankj.utilcode.util.LogUtils
import com.example.wangandroid.adapter.ProjectFagmentStameAdapter
import com.example.wangandroid.data.model.ProjectViewModel

import com.example.wangandroid.databinding.FramentprojecttabBinding
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator


class ProjectFragment : Fragment() {

  private lateinit var mineViewModel: ProjectViewModel
  private lateinit var _binding: FramentprojecttabBinding
  private val binding get() = _binding
  var adapter: ProjectFagmentStameAdapter? = null

    @SuppressLint("SuspiciousIndentation")
    override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
  ): View? {
    mineViewModel =
            ViewModelProvider(this).get(ProjectViewModel::class.java)
    _binding = FramentprojecttabBinding.inflate(inflater, container, false)
    val root: View = binding.root
    return root
  }

  fun initView(){
    mineViewModel._projectdata.observe(this, Observer {
      //获得项目分类
      LogUtils.d("tag==="+it.toString())
      if (it != null) {
        if(_binding.vp2.adapter==null){
         adapter = ProjectFagmentStameAdapter(this,it)
          _binding.vp2.adapter = adapter
          TabLayoutMediator(_binding.tl,_binding.vp2,true,object :TabLayoutMediator.TabConfigurationStrategy{
            override fun onConfigureTab(tab: TabLayout.Tab, position: Int) {
             tab.text = it.data[position].name
            }
          }).attach()
        }else{
          adapter!!.data = it
          adapter!!.notifyDataSetChanged()
        }
      }
    })
    mineViewModel.getProjectData()
  }

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    initView()
  }
}


